﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionDAL;
using SolutionModel;

namespace SolutionBLL
{
    public class CommonClassBLL
    {
        #region 通过类型获取该类型的所有分类
        /// <summary>
        /// 通过类型获取该类型的所有分类
        /// </summary>
        /// <param name="cctype">CommonClassType 枚举分类</param>
        /// <returns></returns>
        public List<TreeGridModel> GetListByType(CommonClassType cctype)
        {
            List<TreeGridModel> listModel = new List<TreeGridModel>();
            GetListByType(cctype, String.Empty);
            return listModel;
        }
        #endregion

        #region 通过类型获取该类型的所有分类
        /// <summary>
        /// 通过类型获取该类型的所有分类
        /// </summary>
        /// <param name="cctype">CommonClassType 枚举分类</param>
        /// <returns></returns>
        public List<TreeGridModel> GetListByType(CommonClassType cctype ,String delID )
        {
            CommonClassDAL dal = new CommonClassDAL();
            List<CommonClass> listClass = dal.GetClassByType(cctype);
            List<TreeGridModel> listModel = new List<TreeGridModel>();
            listClass.ForEach(o =>
            {
                if (String.IsNullOrEmpty(delID) || !("|"+ o.CCPath + "|").Contains("|" + delID + "|"))
                {
                    listModel.Add(new TreeGridModel()
                    {
                        ID = o.CCID,
                        Name = o.CCName,
                        EnName = o.CCEnName,
                        PID = o.CCPID,
                        Path = o.CCPath,
                        Depth = o.CCDepth,
                        Order = o.CCOrder,
                        Tools = "<span onclick=\"editItem('" + o.CCID + "')\" style=\"cursor: pointer; margin: 0px 10px\">编辑</span>",
                        CCType = o.CCType
                    });
                }
            });
            listModel.ForEach(o =>
            {
                o.children = listModel.Where(p => p.PID == o.ID).ToList<TreeGridModel>();
            });
            listModel = listModel.Where(o => o.Depth == 1).ToList();
            return listModel;
        }
        #endregion

        /// <summary>
        /// 添加一条分类数据
        /// </summary>
        /// <param name="cc">分类数据</param>
        /// <returns></returns>
        public MessageInfoByFormModel Insert(CommonClass cc)
        {
            CommonClassDAL dal = new CommonClassDAL();
            MessageInfoByFormModel sim = new MessageInfoByFormModel();
            try
            {
                int rs = dal.Insert(cc);
                if (rs > 0)
                {
                    sim.info = "分类添加成功！";
                    sim.status = "y";
                    return sim;
                }
                else
                {
                    sim.info = "分类添加失败！";
                    sim.status = "n";
                    return sim;
                }
            }
            catch (Exception ex)
            {
                throw ex;
                sim.info = "分类添加失败！错误信息：" + ex.Message;
                sim.status = "n";
                return sim;
            }
        }

        /// <summary>
        /// 添加一条分类数据
        /// </summary>
        /// <param name="cc">分类数据</param>
        /// <returns></returns>
        public MessageInfoByFormModel Update(CommonClass cc)
        {
            CommonClassDAL dal = new CommonClassDAL();
            MessageInfoByFormModel sim = new MessageInfoByFormModel();
            try
            {
                int rs = dal.Update(cc);
                if (rs > 0)
                {
                    sim.info = "分类保存成功！";
                    sim.status = "y";
                    return sim;
                }
                else
                {
                    sim.info = "分类保存失败！";
                    sim.status = "n";
                    return sim;
                }
            }
            catch (Exception ex)
            {
                sim.info = "分类保存失败！错误信息：" + ex.Message;
                sim.status = "n";
                return sim;
            }
        }
    }
}
